<template>
	<div class="addProject business-workAdd">
		<div class="addForm zll-form">
			<el-form :model="addForm" :rules="rules" ref="addForm" class="demo-ruleForm">
				<div class="nav_list">
					<div class="_title"><i class="el-icon-s-promotion"></i>个人信息</div>
					<el-row :gutter="40">
						<el-col :span="12">
							<el-form-item class="formList" prop="jobCategory" label="岗位类别：">
								<el-select clearable class="input_right" v-model="addForm.jobCategory" :disabled="type == 'see'">
									<el-option label="管理" value="A"></el-option>
									<el-option label="操作" value="Y"></el-option>
								</el-select>
							</el-form-item>
							<el-form-item class="formList" prop="data2" label="部门：">
								<el-select clearable class="input_right" v-model="addForm.deptId" :disabled="type == 'see'">
									<el-option v-for="item in depts" :key="item.id" :label="item.deptName" :value="item.id"></el-option>
								</el-select>
							</el-form-item>
							<el-form-item class="formList" prop="employeeName" label="姓名：">
								<el-input clearable class="input_right"  v-model="addForm.employeeName" :disabled="type == 'see'" ></el-input>
							</el-form-item>
							<el-form-item class="formList" prop="gender" label="性别：">
								<el-select clearable class="input_right" v-model="addForm.gender" :disabled="type == 'see'">
									<el-option label="男" value="M"></el-option>
									<el-option label="女" value="F"></el-option>
								</el-select>
							</el-form-item>
							<el-form-item class="formList" prop="birthday" label="出生年月：">
								<!--                                <el-input clearable class="input_right"  v-model="addForm.birthday" :disabled="type == 'see'" type="date"></el-input>-->
								<el-date-picker
									format="yyyy-MM-dd"
									value-format="yyyy-MM-dd"
									clearable class="input_right"  v-model="addForm.birthday" :disabled="type == 'see'" type="date"></el-date-picker>
							</el-form-item>
							<el-form-item class="formList" prop="identitytionId" label="身份证号：">
								<el-input clearable class="input_right"  v-model="addForm.identitytionId"  :disabled="type == 'see'" ></el-input>
							</el-form-item>
							<el-form-item class="formList" prop="healthStatus" label="健康鉴定：">
								<el-select clearable class="input_right" v-model="addForm.healthStatus" :disabled="type == 'see'">
									<el-option label="是" value="0"></el-option>
									<el-option label="否" value="1"></el-option>
								</el-select>
							</el-form-item>
						</el-col>
						<el-col :span="12">
							<div class="_hr_">
<!--								<img-upload ref="personnelRef" limit="1" :imgList="headImage" :edit="type == 'see'" type="headImg" :isImgSave="true" :hidden="true" ></img-upload>-->
								<img-upload ref="personnelRef" limit="1" :imgList="headImage" :edit="type !== 'see'" type="headImg" :isImgSave="true" :hidden="true" ></img-upload>
							</div>
							<el-form-item class="formList" prop="onboardingStatus" label="状态：">
								<el-select clearable class="input_right" v-model="addForm.onboardingStatus" :disabled="type == 'see'">
									<el-option label="试用在职" value="0"></el-option>
									<el-option label="合格在职" value="1"></el-option>
									<el-option label="离职" value="2"></el-option>
								</el-select>
							</el-form-item>
							<el-form-item class="formList" prop="jobyear" label="工龄：">
								<el-input clearable class="input_right"  v-model="addForm.jobyear"  disabled ></el-input>
							</el-form-item>
							<el-form-item class="formList" prop="createrName" label="注册日期：">
								<!--                                <el-date-picker clearable class="input_right"  v-model="addForm.createrName" disabled type="date"></el-date-picker>-->
								<el-input clearable class="input_right"  v-model="addForm.createrName"  disabled ></el-input>
							</el-form-item>
						</el-col>
					</el-row>
				</div>
				<div class="nav_list">
					<div class="_title"><i class="el-icon-s-promotion"></i>入职信息</div>
					<el-row :gutter="40">
						<el-col :span="12">
							<el-form-item class="formList" prop="joinedDate" label="入职日期：">
								<el-date-picker
									format="yyyy-MM-dd"
									value-format="yyyy-MM-dd"
									clearable class="input_right"  v-model="addForm.joinedDate" :disabled="type == 'see'" type="date"></el-date-picker>
							</el-form-item>
							<el-form-item class="formList" prop="whetherProbation" label="是否试用：">
								<el-select clearable class="input_right" v-model="addForm.whetherProbation" :disabled="type == 'see'" @change="changeShiYongType">
									<el-option label="是" value="0"></el-option>
									<el-option label="否" value="1"></el-option>
								</el-select>
							</el-form-item>
							<el-form-item class="formList" prop="endDate" label="结束日期：" v-if="isShiYong">
								<el-date-picker
									format="yyyy-MM-dd"
									value-format="yyyy-MM-dd"
									clearable class="input_right"  v-model="addForm.endDate" :disabled="type == 'see'" type="date"></el-date-picker>
							</el-form-item>
						</el-col>

						<el-col :span="12">
							<el-form-item class="formList"></el-form-item>
							<el-form-item class="formList" prop="probationConclusion" label="试用结论：" v-if="isShiYong">
								<el-select clearable class="input_right" v-model="addForm.probationConclusion" :disabled="type == 'see'" @change="changeProbationConclusion">
									<el-option label="合格" value="0"></el-option>
									<el-option label="不合格" value="1"></el-option>
								</el-select>
							</el-form-item>
							<el-form-item class="formList" prop="ensurePeopleId" label="确认人：" v-if="isShiYong">
								<el-input clearable class="input_right"  v-model="addForm.ensuerName"  disabled ></el-input>
							</el-form-item>
						</el-col>
					</el-row>

					<el-row :gutter="40">
						<el-col :span="12">
							<el-form-item class="formList" prop="terminateDate" label="离职日期：" >
								<el-date-picker
									format="yyyy-MM-dd"
									value-format="yyyy-MM-dd"
									clearable class="input_right"  v-model="addForm.terminateDate" :disabled="type == 'see'" type="date" @change="changeLeftDate"></el-date-picker>
							</el-form-item>
							<el-form-item class="formList" prop="remarks" label="备注：" >
								<el-input clearable class="input_right"  v-model="addForm.remarks" maxlength="20" :disabled="type == 'see'" show-word-limit></el-input>
							</el-form-item>
						</el-col>
						<el-col :span="12">
							<el-form-item class="formList"></el-form-item>
							<el-form-item class="formList" prop="operatorName" label="经办人：" >
								<el-input clearable class="input_right"  v-model="addForm.operatorName"  disabled ></el-input>
							</el-form-item>
						</el-col>
					</el-row>
				</div>
			</el-form>
		</div>
	</div>
</template>
<script>
import imgUpload from '../../../../system/visual-img/imgUpload.vue';
import {getStore} from "../../../../../plugins/localStore";
import {formatDate} from "../../../../../plugins/formatDate";

export default {
    props: ['editData','type','deptList'],
    data(){
        var checkIdentitytionId = (rule, value, callback) => {
            if (!value) {
                return callback(new Error("身份证号不能为空"));
            }
            if (!/(^\d{15}$)|(^\d{17}(\d|X|x)$)/.test(value)) {
                callback(new Error("你输入的身份证长度或格式错误"));
            }
            //身份证城市
            var aCity = {
                11: "北京",
                12: "天津",
                13: "河北",
                14: "山西",
                15: "内蒙古",
                21: "辽宁",
                22: "吉林",
                23: "黑龙江",
                31: "上海",
                32: "江苏",
                33: "浙江",
                34: "安徽",
                35: "福建",
                36: "江西",
                37: "山东",
                41: "河南",
                42: "湖北",
                43: "湖南",
                44: "广东",
                45: "广西",
                46: "海南",
                50: "重庆",
                51: "四川",
                52: "贵州",
                53: "云南",
                54: "西藏",
                61: "陕西",
                62: "甘肃",
                63: "青海",
                64: "宁夏",
                65: "新疆",
                71: "台湾",
                81: "香港",
                82: "澳门",
                91: "国外"
            };
            if (!aCity[parseInt(value.substr(0, 2))]) {
                callback(new Error("你的身份证地区非法"));
            }
            // 出生日期验证
            var sBirthday = ( value.substr(6, 4) +  "-" + Number(value.substr(10, 2)) + "-" + Number(value.substr(12, 2)) ).replace(/-/g, "/"), d = new Date(sBirthday);
            if ( sBirthday != d.getFullYear() + "/" + (d.getMonth() + 1) + "/" + d.getDate() ) {
                callback(new Error("身份证上的出生日期非法"));
            }
            // 身份证号码校验
            var sum = 0,
                weights = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2],
                codes = "10X98765432";
            for (var i = 0; i < value.length - 1; i++) {
                sum += value[i] * weights[i];
            }
            var last = codes[sum % 11]; //计算出来的最后一位身份证号码
            if (value[value.length - 1] != last) {
                callback(new Error("你输入的身份证号非法"));
            }
            callback();
        };
        return {
			isShiYong: false,
			addForm: {
				id: '',
				jobCategory:'',
				deptId:'',
				employeeName:'',
				gender:'',
				birthday:'',
				healthStatus: '',
				identitytionId:'',
				onboardingStatus:'',
				jobyear:'',
				createrName:'',
				joinedDate:'',
				whetherProbation: '',
				endDate: '',
				remarks: '',
				terminateDate: '',
				operatorName: '',
				ensuerName: '',
				probationConclusion: '',
				fileBlob: ''

			},

			ensurePeopleId: '',
			ensureDate: '',
			operatorId: '',
			operatorDate: '',

			depts: [],
			headImage:[],
			count: 0,


			rules: {
				jobCategory: [
					{required: true, message: '请选择岗位类别', trigger: 'change'},
				],
				deptId: [
					{required: true, message: '请选择部门', trigger: 'change'},
				],
				employeeName: [
					{required: true, message: '请输入姓名', trigger: 'blur'},
				],
				gender: [
					{required: true, message: '请选择性别', trigger: 'change'},
				],
				whetherProbation: [
					{required: true, message: '请选择是否试用', trigger: 'change'},
				],
				birthday: [
					{required: true, message: '请输入出生日期', trigger: 'change'},
				],
				identitytionId: [
					{required: true, validator: checkIdentitytionId, trigger: 'blur'},
				],
				healthStatus: [
					{required: true, message: '请选择健康鉴定', trigger: 'change'},
				],
				joinedDate: [
					{required: true, message: '请选择入职日期', trigger: 'change'},
				],
			}
        }
    },
    methods: {
		changeShiYongType(val) {
			if(val == '1'){
				this.isShiYong = false;
			}else {
				this.isShiYong = true;
			}
		},

		changeProbationConclusion(val){
			let self = this;
			let user = getStore();

			if (val == '0'){
				self.addForm.onboardingStatus = '1';//合格
			} else if (val == '1'){
				self.addForm.onboardingStatus = '2';//离职
				self.operatorId = user.id;
				self.operatorDate = formatDate(new Date(), 'yyyy-MM-dd hh:mm:ss');
			}

			self.ensurePeopleId = user.id;
			self.ensureDate = formatDate(new Date(), 'yyyy-MM-dd hh:mm:ss');

		},

		changeLeftDate(val){
			let self = this;
			let user = getStore();
			if (val){
				self.operatorId = user.id;
				self.operatorDate = formatDate(new Date(), 'yyyy-MM-dd hh:mm:ss');
				self.onboardingStatus = '2';
			} else{
				self.operatorId = '';
				self.operatorDate = '';
			}

		},

		submitForm() {
			let self = this;
			self.$refs['addForm'].validate((valid) => {
				if (valid) {
					// self.$eventBus.$emit("updateWorkList");
					self.operate();
				} else {
					return false;
				}
			});
		},


		operate() {
			let self = this;
			let user = getStore();
			let url = self.addForm.id ? '/jinl/personnelManage/updatePersonnelManagement' : '/jinl/personnelManage/addPersonnelManagement';

			if (self.addForm.whetherProbation == '1'){
				//不试用  直接入职
				if (self.addForm.terminateDate){//填了离职日期
					self.addForm.onboardingStatus = '2';//变为离职状态
					self.operatorId = user.id;//录入经办人
					self.operatorDate = formatDate(new Date(), 'yyyy-MM-dd hh:mm:ss');

				} else {//没有填离职信息
					self.addForm.onboardingStatus = '1';//直接入职
				}
			}else{
				//试用
				if (self.addForm.probationConclusion){//填写试用结论
					if (!self.addForm.endDate) {//有结论必须有试用期结束日期
						self.$message.warning('请填入结束日期');
						return;
					}

					if (self.addForm.probationConclusion == '1'){
						//如果试用不合格

						if (!self.addForm.terminateDate) {
							self.$message.warning('请填入离职日期');
							return;
						}
						self.operatorId = user.id;
						self.operatorDate = formatDate(new Date(), 'yyyy-MM-dd hh:mm:ss');
					}else{
						//试用合格
						if (self.addForm.terminateDate){//填写了离职日期，直接离职
							self.addForm.onboardingStatus = '2';
						}
					}
				}else {
					self.addForm.onboardingStatus = '0';
				}
			}

			let param = {
				id: self.addForm.id,
				jobCategory: self.addForm.jobCategory,
				deptId: self.addForm.deptId,
				employeeName: self.addForm.employeeName,
				gender: self.addForm.gender,
				employeeName: self.addForm.employeeName,
				birthday: self.addForm.birthday,
				idCard: self.addForm.identitytionId,
				healthStatus: self.addForm.healthStatus,
				onboardingStatus: self.addForm.onboardingStatus,
				joinedDate: self.addForm.joinedDate,
				whetherProbation: self.addForm.whetherProbation,
				probationConclusion: self.addForm.probationConclusion,
				endDate: self.addForm.endDate,
				terminateDate: self.addForm.terminateDate,
				remarks: self.addForm.remarks,
				ensurePeopleId: self.ensurePeopleId,
				ensureDate: self.ensureDate,
				operatorId: self.operatorId,
				operatorDate: self.operatorDate

			};
			if (!self.addForm.id){
				param.createUserId = user.id;
			}
			let images = self.$refs.personnelRef.getImages();
			if (images.length == 0 || !images[0].id){
				self.$message.warning('请上传员工头像');
				return;
			}
			param.headId = images[0].id;
			if (this.count == 0) {
				self.count++;
				self.$http({
					url: url,
					method: "post",
					data: JSON.stringify(param),
					dataType: 'json',
					contentType: 'application/json',
				}).then(resp => {
					if (resp.success) {
						self.$message.success('操作成功');
						self.$emit('refreshData');
						return;
					}
					self.$message.error(resp.message);
					self.count--;
				})
			}
		},



    },
    mounted(){
        
    },
    watch: {
        // ['addForm.identitytionId'](val) {
        //     this.addForm.data5 = val.substr(6, 8).replace(/(.{4})(.{2})/, '$1-$2-')
        // }

		editData(val) {
			this.depts = JSON.parse(JSON.stringify(this.deptList || []));
			this.headImage = [];

			if (Object.getOwnPropertyNames(val).length > 1){
				this.isShiYong = val.whetherProbation== '1' ? false : true;
				this.ensurePeopleId= val.ensurePeopleId;
				this.operatorId= val.operatorId;
				this.ensureDate= val.ensureDate;
				this.operatorDate= val.operatorDate;

				this.addForm.id = val.id;
				this.addForm.identitytionId = val.idCard;//身份证
				this.addForm.jobCategory = val.jobCategory;
				this.addForm.deptId = val.deptId;
				this.addForm.employeeName = val.employeeName;
				this.addForm.gender = val.gender;
				this.addForm.birthday = val.birthday;
				this.addForm.healthStatus = val.healthStatus;
				this.addForm.onboardingStatus = val.onboardingStatus;
				this.addForm.jobyear = val.jobyear;
				this.addForm.createrName = val.createrName;
				this.addForm.joinedDate = val.joinedDate;
				this.addForm.whetherProbation = val.whetherProbation;
				this.addForm.endDate = val.endDate;
				this.addForm.probationConclusion = val.probationConclusion;
				this.addForm.ensuerName = val.ensuerName;
				this.addForm.terminateDate = val.terminateDate;
				this.addForm.remarks = val.remarks;
				this.addForm.operatorName = val.operatorName;

				let file = {fileBlob:val.fileBlob||'',id:val.headId,fileName:'headImg'};
				if (file.id){
					this.headImage.push(file);
				}
			}

		}


    },
    components: {imgUpload}
}
</script>
<style lang="scss" scoped>
@import "@/style/dialog.scss";
.business-workAdd.addProject {
    .nav_list {
        margin-top: 20px;
        ._title {
            line-height: 40px;
            color: #00D1D0;
            i {
                font-size: 16px;
                margin-right: 5px;
            }
        }
        .formList {
            margin-bottom: 8px;
            height: 32px;
        }
    }
}
</style>